package com.allen.app.common.config.swagger.reader;

import com.allen.app.common.config.swagger.common.HideType;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.members.RawField;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;
import springfox.documentation.schema.Model;

/**
 * @author Leon (234239150@qq.com) created in 15:33 18.9.11
 */
public class ApiReaderContext {

    @Getter
    private Map<String, Model> models = new HashMap<>();

    @Getter
    private Map<String, ApiHideProperty> hideProperties = new HashMap<>();

    @Getter
    private Map<String, ResolvedType> enumAndHideParameter = new HashMap<>();


    public void add(String modelRef, ResolvedType resolvedType, String[] hideProperties, HideType hideType) {
        this.hideProperties.put(modelRef, ApiHideProperty.builder().resolvedType(resolvedType).hideProperties(hideProperties).hideType(hideType).build());
    }

    public void addenumAndHideParameter(String modelRef, ResolvedType resolvedType) {
        this.enumAndHideParameter.put(modelRef, resolvedType);
    }


    @Data
    @Builder
    @ToString
    public static class ApiHideProperty {
        private ResolvedType resolvedType;
        private String[] hideProperties;
        private HideType hideType;

        public List<String> modelFields() {
            return resolvedType.getMemberFields()
                    .parallelStream().map(RawField::getName)
                    .filter(field -> !Arrays.asList(hideProperties).contains(field))
                    .collect(Collectors.toList());
        }
    }
}
